﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD.Models;

namespace SAD.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        DataClasses1DataContext data = new DataClasses1DataContext();

        public ActionResult Index()
        {
            var user = (from o in data.Accounts select o).ToList();
            return View(user);

        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string acc, string pass)
        {
            if (ModelState.IsValid)
            {
                var U = (from u in data.Accounts where u.username == acc where u.password == pass select u).FirstOrDefault();
                if (U == null)
                {
                    //ViewBag.erro = "Invalid Login! Try Again";
                    ViewBag.erro= "The user name or password provided is incorrect.";
                    return View();
                }
                else
                {
                    Session["User"] = U.username;
                    Session["Role"] = U.role;
                    Session["AccID"] = U.accountID;
                    return RedirectToAction("Index", "Food");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
            return View();
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(Account user)
        {

            bool isValid = true;
            ViewBag.Erro = "Error ! ";
            if (string.IsNullOrEmpty(user.username))
            {
                ViewBag.Erro = "You must enter User name ";
                return View();
            }
            if (string.IsNullOrEmpty(user.password))
            {
                ViewBag.Erro = "You must enter Password ";
                return View();
            }
            if (string.IsNullOrEmpty(user.fullname))
            {
                ViewBag.Erro = "You must enter Name ";
                return View();
            }

            if (string.IsNullOrEmpty(user.address))
            {
                ViewBag.Erro = "You must enter Address ";
                return View();
            }

            if (string.IsNullOrEmpty(user.phone))
            {
                ViewBag.Erro = "You must enter phone ";
                return View();
            }

            var user1 = data.Accounts.SingleOrDefault(x => x.username == user.username);
            if (user1 != null)
            {
                ViewBag.Erro = "This user name already exists, please choose another name! ";
                return View();
            }
           
            user.role = "user";

            if (!isValid)
                return View();
            data.Accounts.InsertOnSubmit(user);
            data.SubmitChanges();
            return RedirectToAction("Login");
        }

        /// <summary>
        /// user logout -> clear session
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOut()
        {
            Session["User"] = null;
            Session["Role"] = null;
            Session["AccID"] = null;
            return RedirectToAction("Index", "Food");
        }

        /// <summary>
        /// update user for admin
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ViewResult Details(int id)
        {
            Account account = data.Accounts.Single(x => x.accountID == id);
            return View(account);
        }

        /// <summary>
        /// update user for admin
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Update(int id)
        {
            Account account = data.Accounts.Single(x => x.accountID == id);
            return View(account);
        }
        [HttpPost]
        public ActionResult Update(Account acc)
        {
            var updateAcc = data.Accounts.Single(x => x.accountID == acc.accountID);
            updateAcc.address = acc.address;
            updateAcc.fullname = acc.fullname;
            updateAcc.password = acc.password;
            updateAcc.phone = acc.phone;
            updateAcc.role = acc.role;
            data.SubmitChanges();
            return RedirectToAction("Index");
        }

        /// <summary>
        /// delete User for admin
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            Account account = data.Accounts.Single(x => x.accountID == id);
            return View(account);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirm (int id)
        {
            Account account = data.Accounts.Single(x => x.accountID == id);
            data.Accounts.DeleteOnSubmit(account);
            data.SubmitChanges();
            return RedirectToAction("Index");
        }
    }
}
